package com.quiz.bis.mapper;


import com.quiz.bis.domain.model.ExamGroupInvite;
import com.quiz.common.mybatis.core.mapper.BaseMapperPlus;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


@Mapper
public interface ExamGroupInviteMapper extends BaseMapperPlus<ExamGroupInvite> {
    @Select("SELECT * FROM exam_group_invites " +
            "WHERE group_id = #{groupId} AND code = #{code} AND del_flag = 0 " +
            "AND (expire_time IS NULL OR expire_time > NOW()) " +
            "AND (max_uses IS NULL OR used_count < max_uses)")
    ExamGroupInvite selectValidInvite(@Param("groupId") Long groupId, @Param("code") String code);

    @Update("UPDATE exam_group_invites SET used_count = used_count + 1 WHERE id = #{id}")
    int updateUsedCount(@Param("id") Long id);
}
